package com.situ.web.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.situ.web.dao.IBanjiDao;
import com.situ.web.entity.Banji;
import com.situ.web.entity.Course;
import com.situ.web.util.JDBCUtil;
import com.situ.web.util.ModelConvertUtil;

public class BanjiDaoImpl implements IBanjiDao{

	@Override
	public List<Banji> selectAll() {
		ArrayList<Banji> list = new ArrayList<Banji>();
		Connection connection = null;
		Statement statement = null;
		ResultSet resultSet = null;
		try {
			connection = JDBCUtil.getConnection();
			String sql = "SELECT id,`name` FROM banji";
			statement = connection.createStatement();
			resultSet = statement.executeQuery(sql);
			while (resultSet.next()) {
				int id = resultSet.getInt("id");
				String name = resultSet.getString("name");
				Banji banji = new Banji(id, name);
				list.add(banji);
			}
			
			for (Banji banji : list) {
				System.out.println(banji);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(connection, statement, resultSet);
		}
		return list;
	}

	@Override
	public List<Banji> search(String searchName) {
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		ArrayList<Banji> list = new ArrayList<Banji>();
		try {
			connection = JDBCUtil.getConnection();
			String sql = "Select id,`name` from banji Where name like ? ";
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setString(1, "%"+searchName+"%");
			
			resultSet = preparedStatement.executeQuery();
			while (resultSet.next()) {
				int id = resultSet.getInt("id");
				String name = resultSet.getString("name");
				Banji banji = new Banji(id, name);
				list.add(banji);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(connection, preparedStatement, resultSet);
		}
		return list;
	}

	@Override
	public Integer insert(Banji banji) {
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		int count = 0;
		try {
			connection = JDBCUtil.getConnection();
			String sql = "INSERT INTO banji(`name`) VALUES(?)";
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setString(1, banji.getName());
			count = preparedStatement.executeUpdate();
			if (count == 1) {
				System.out.println("插入成功");
			} else {
				System.out.println("插入失败");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(connection, preparedStatement, resultSet);
		}
		return count;
	}

	@Override
	public Integer update(Banji banji) {
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		int count = 0;
		try {
			connection = JDBCUtil.getConnection();
			String sql = "update banji set `name` = ? where id = ?";
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setString(1, banji.getName());
			preparedStatement.setInt(2, banji.getId());
			count = preparedStatement.executeUpdate();
			if (count == 1) {
				System.out.println("更改成功");
			} else {
				System.out.println("更改失败");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(connection, preparedStatement, null);
		}
		return count;
	}

	@Override
	public Integer deleteById(Integer id) {
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		int count =0;
		try {
			connection = JDBCUtil.getConnection();
			String sql = "delete from banji where id = ?";
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setInt(1, id);
			 count = preparedStatement.executeUpdate();
			if (count == 1) {
				System.out.println("删除成功");
			}else {
				System.out.println("删除失败");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			JDBCUtil.close(connection, preparedStatement, null);
		}
		return count;
	}

	@Override
	public List<Banji> selectByName(String searchName) {
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		ArrayList<Banji> list = new ArrayList<Banji>();
		try {
			connection = JDBCUtil.getConnection();
			String sql = "Select id,`name` from banji Where name = ? ";
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setString(1, searchName);
			
			resultSet = preparedStatement.executeQuery();
			while (resultSet.next()) {
				int id = resultSet.getInt("id");
				String name = resultSet.getString("name");
				Banji banji = new Banji(id, name);
				list.add(banji);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(connection, preparedStatement, resultSet);
		}
		return list;
	}

	@Override
	public List<Banji> selectById() {
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		ArrayList<Banji> list = new ArrayList<Banji>();
		try {
			connection = JDBCUtil.getConnection();
			String sql = "Select * from banji ";
			preparedStatement = connection.prepareStatement(sql);
			resultSet = preparedStatement.executeQuery();
			while (resultSet.next()) {
				String name = resultSet.getString("name");
				int id = resultSet.getInt("id");
				Banji banji1 = new Banji(id,name);
				list.add(banji1);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(connection, preparedStatement, resultSet);
		}
		return list;
	}

	@Override
	public Banji selectById2(Integer iD) {
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		Banji banji = new Banji();
		try {
			connection = JDBCUtil.getConnection();
			String sql = "Select id,`name` from banji Where id =? ";
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setInt(1, iD);
			resultSet = preparedStatement.executeQuery();
			while (resultSet.next()) {
				int id = resultSet.getInt("id");
				String name = resultSet.getString("name");
				banji = new Banji(id, name);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(connection, preparedStatement, resultSet);
		}
		return banji;
	}

	@Override
	public Integer getTotalCount() {
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		int count = 0;
		try {
			connection = JDBCUtil.getConnection();
			String sql = "SELECT COUNT(*) FROM banji";
			preparedStatement = connection.prepareStatement(sql);
			resultSet = preparedStatement.executeQuery();
			if (resultSet.next()) {
				count = resultSet.getInt("count(*)");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			JDBCUtil.close(connection, preparedStatement, resultSet);
		}
		System.out.println(count);
		return count;
	}

	@Override
	public List<Banji> pageList(int offset, Integer pageSize) {
		List<Banji> list = new ArrayList<Banji>();
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		try {
			connection = JDBCUtil.getConnection();
			String sql = "SELECT * FROM banji LIMIT ?,?";
			System.out.println(sql);
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setInt(1, offset);
			preparedStatement.setInt(2, pageSize);
			resultSet = preparedStatement.executeQuery();
			while (resultSet.next()) {
				int id = resultSet.getInt("id");
				String name = resultSet.getString("name");
				Banji banji = new Banji(id, name);
				list.add(banji);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			JDBCUtil.close(connection, preparedStatement, resultSet);
		}
		return list;
	}

	@Override
	public List<Map<String, Object>> selectBanjiCourseInfo() {
		List<Map<String, Object>> list = null;
		Connection connection = null;
		Statement statement = null;
		ResultSet resultSet = null;
		try {
			connection = JDBCUtil.getConnection();
			String sql = "SELECT b.id,b.name,c.name AS courseName,c.credit FROM banji AS b INNER JOIN banji_course AS bc ON b.id = bc.banji_id INNER JOIN course AS c ON c.id = bc.course_id";
			statement = connection.createStatement();
			resultSet = statement.executeQuery(sql);
			list = ModelConvertUtil.converToList(resultSet);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(connection, statement, resultSet);
		}
		return list;
	}

	@Override
	public ArrayList<Course> selectCourse() {
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		ArrayList<Course> list = new ArrayList<Course>();
		try {
			connection = JDBCUtil.getConnection();
			String sql = "Select * from Course ";
			preparedStatement = connection.prepareStatement(sql);
			resultSet = preparedStatement.executeQuery();
			while (resultSet.next()) {
				String name = resultSet.getString("name");
				int id = resultSet.getInt("id");
				int credit = resultSet.getInt("credit");
				Course course = new Course(id,name,credit);
				list.add(course);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(connection, preparedStatement, resultSet);
		}
		return list;
	}
}
